// pages/game/game.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    modalHidden: true,
    showModalStatus: false,
    nextQuestion: false,
    dialog: {
      duanwei: '童生',
      money: 300,
      level: '9'
    },
    gameInfo:
    {
      level: '9',
      tishi: '嘿嘿嘿嘿嘿心',
      riddle: '乌贼',
      unMakeRiddle: '嘴里说得很好，心里想的却是另一套。指心口不一致。',
      color: '#fff',
      choose: [' ', ' '],
      hanzi: [{ num: '1', zi: '为', isChecked: false },
      { num: '2', zi: '是', isChecked: false },
      { num: '3', zi: '话', isChecked: false },
      { num: '4', zi: '多', isChecked: false },
      { num: '5', zi: '乌', isChecked: false },
      { num: '6', zi: '醉', isChecked: false },
      { num: '7', zi: '最', isChecked: false },
      { num: '8', zi: '嘴', isChecked: false },
      { num: '9', zi: '时', isChecked: false },
      { num: '10', zi: '石', isChecked: false },
      { num: '11', zi: '事', isChecked: false },
      { num: '12', zi: '情', isChecked: false },
      { num: '13', zi: '贼', isChecked: false },
      { num: '14', zi: '错', isChecked: false },
      { num: '15', zi: '无', isChecked: false },
      { num: '16', zi: '心', isChecked: false },
     
      ]
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.getStorage({
      key: 'yonghu',
      success: function (res) {
        var title = "第" + res.data.level + "关";
        //console.log(title);
        wx.setNavigationBarTitle({
          title: title
        })
      }
    })
  },

  modalChange: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  modalChange2: function (e) {
    this.setData({
      showModalStatus: false
    });
    this.setData({
      nextQuestion: true
    });
    var money = this.data.dialog.money + 5;
    console.log(money);
    this.setData({
      'dialog.money': money
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  close: function (e) {
    if (this.data.nextQuestion == false) {
      var id = e.currentTarget.dataset.index;
      var xuan = this.data.gameList1[0].choose;
      xuan[id] = '';
      this.setData({
        'gameList1[0].choose': xuan
      });
      var num = e.currentTarget.dataset.num - 1;
      var thisData = this.data.gameList1[0].hanzi[num];
      thisData.isChecked = false;
      var str = "gameList1[0].hanzi[" + num + "]";
      var that = this;
      that.setData({
        [str]: thisData
      });
      console.log(num);
    } else {
      var id = e.currentTarget.dataset.index;
      var xuan = this.data.gameList2[0].choose;
      xuan[id] = '';
      this.setData({
        'gameList2[0].choose': xuan
      });
      var num = e.currentTarget.dataset.num - 1;
      var thisData = this.data.gameList2[0].hanzi[num];
      thisData.isChecked = false;
      var str = "gameList2[0].hanzi[" + num + "]";
      var that = this;
      that.setData({
        [str]: thisData
      });
      console.log(num);
    }
  },

  /**
   * 用户点击事件
   */
  clickTap: function (e) {
    if (this.data.nextQuestion == false) {
      var id = e.currentTarget.dataset.num - 1;
      var thisData = this.data.gameList1[0].hanzi[id];
      thisData.isChecked = true;
      var str = "gameList1[0].hanzi[" + id + "]";
      var that = this;
      var xuan = this.data.gameList1[0].choose;
      var arr = '';
      for (var i = 0; i < xuan.length; i++) {
        //console.log(xuan[i]);
        if (xuan[0] == '') {
          //console.log(xuan[0]);
          that.setData({
            [str]: thisData
          });
          xuan[0] = this.data.gameList1[0].hanzi[id];
          that.setData({
            'gameList1[0].choose': xuan
          });
          break;
        } else if (xuan[1] == '') {
          //console.log(xuan[1]);
          that.setData({
            [str]: thisData
          });
          xuan[1] = this.data.gameList1[0].hanzi[id];
          that.setData({
            'gameList1[0].choose': xuan
          });
          break;
        } else if (xuan[2] == '') {
          //console.log(xuan[2]);
          that.setData({
            [str]: thisData
          });
          xuan[2] = this.data.gameList1[0].hanzi[id];
          that.setData({
            'gameList1[0].choose': xuan
          });
          break;
        } else if (xuan[3] == '') {
          //console.log(xuan[3]);
          that.setData({
            [str]: thisData
          });
          xuan[3] = this.data.gameList1[0].hanzi[id];
          that.setData({
            'gameList1[0].choose': xuan
          });
          break;
        }
      }
      if (xuan[0] != '' && xuan[1] != '' && xuan[2] != '' && xuan[3] != '') {
        //console.log(xuan);
        for (var y = 0; y < xuan.length; y++) {
          arr = arr + xuan[y].zi;
        }
        console.log(arr);
        if (arr == this.data.gameList1[0].chengyu) {
          that.setData({
            showModalStatus: true
          });
        } else {
          that.setData({
            'gameList1[0].color': 'red'
          });
          that.setData({
            modalHidden: false
          });
        }
      }
      //console.log(this.data);
    } else {
      var id = e.currentTarget.dataset.num - 1;
      var thisData = this.data.gameList2[0].hanzi[id];
      thisData.isChecked = true;
      var str = "gameList2[0].hanzi[" + id + "]";
      var that = this;
      var xuan = this.data.gameList2[0].choose;
      var arr = '';
      for (var i = 0; i < xuan.length; i++) {
        //console.log(xuan[i]);
        if (xuan[0] == '') {
          //console.log(xuan[0]);
          that.setData({
            [str]: thisData
          });
          xuan[0] = this.data.gameList2[0].hanzi[id];
          that.setData({
            'gameList2[0].choose': xuan
          });
          break;
        } else if (xuan[1] == '') {
          //console.log(xuan[1]);
          that.setData({
            [str]: thisData
          });
          xuan[1] = this.data.gameList2[0].hanzi[id];
          that.setData({
            'gameList2[0].choose': xuan
          });
          break;
        } else if (xuan[2] == '') {
          //console.log(xuan[2]);
          that.setData({
            [str]: thisData
          });
          xuan[2] = this.data.gameList2[0].hanzi[id];
          that.setData({
            'gameList2[0].choose': xuan
          });
          break;
        } else if (xuan[3] == '') {
          //console.log(xuan[3]);
          that.setData({
            [str]: thisData
          });
          xuan[3] = this.data.gameList2[0].hanzi[id];
          that.setData({
            'gameList2[0].choose': xuan
          });
          break;
        }
      }
      if (xuan[0] != '' && xuan[1] != '' && xuan[2] != '' && xuan[3] != '') {
        //console.log(xuan);
        for (var y = 0; y < xuan.length; y++) {
          arr = arr + xuan[y].zi;
        }
        console.log(arr);
        if (arr == this.data.gameList2[0].chengyu) {
          that.setData({
            showModalStatus: true
          });
        } else {
          that.setData({
            'gameList2[0].color': 'red'
          });
          that.setData({
            modalHidden: false
          });
        }
      }
    }
  }
})